package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息，不包含用户的角色
        if (user == null) {
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色ID查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }

    /**
     * 分页查询用户
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<User> page;
        if (queryString == null) {
            queryString = "%%";
            page = userDao.pageQuery(queryString);
        } else {
            queryString = "%" + queryString + "%";
            page = userDao.pageQuery(queryString);
        }
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    //查询侧边栏展示菜单:
    public List<Menu> findMenuByUsername(String username) {
        List<Menu> menuList = userDao.findMenuByUsername(username);
        if (menuList != null && menuList.size() > 0) {
            return menuList;
        }
        return null;
    }

    /**
     * 添加用户
     *
     * @param user
     * @param checkitemIds
     */
    @Override
    public void addUser(User user, List<Integer> checkitemIds) {
        //密码加密
        String password = user.getPassword();
        String gensalt = BCrypt.gensalt();
        String newPassword = BCrypt.hashpw(password, gensalt);
        user.setPassword(newPassword);
        //添加至用户表
        userDao.add(user);
        //添加用户角色中间表
        Integer uid = user.getId();
        if (checkitemIds != null && checkitemIds.size() > 0) {
            for (Integer roleId : checkitemIds) {
                HashMap<String, Integer> idMap = new HashMap<>();
                idMap.put("user_id", uid);
                idMap.put("role_id", roleId);
                userDao.addUserRoleIds(idMap);
            }
        }
    }
}
